package com.ruoyi.fly.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.fly.domain.*;
import com.ruoyi.fly.domain.vo.*;
import com.ruoyi.fly.mapper.*;
import com.ruoyi.fly.service.AgentStoreCarService;
import com.ruoyi.fly.service.VehiclesService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 车辆信息Service业务层处理
 *
 * @date 2024-04-25
 */
@Service
public class VehiclesServiceImpl implements VehiclesService {
    @Autowired
    private VehiclesMapper vehiclesMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private AppletServerMapper appletServerMapper;

    @Autowired
    private AppletVehiclesServerMapper appletVehiclesServerMapper;

    @Autowired
    private AppletVehiclesProductMapper appletVehiclesProductMapper;

//    @Autowired
//    private PcVehiclesJoinMapper pcVehiclesJoinMapper;

    @Autowired
    private AgentStoreCarService agentStoreCarService;

    /**
     * 查询车辆信息
     *
     * @param id 车辆信息主键
     * @return 车辆信息
     */
    @Override
    public VehiclesVo selectPcVehiclesById(String id) {
        Vehicles vehicles = vehiclesMapper.selectPcVehiclesById(id);
        VehiclesVo vehiclesVo = new VehiclesVo();
        BeanUtils.copyProperties(vehicles, vehiclesVo);
        if (sysUserService.selectUserById(vehicles.getApplyUser()) != null) {
            vehiclesVo.setApplyUserName(sysUserService.selectUserById(vehicles.getApplyUser()).getNickName());
        }
        return vehiclesVo;
    }

    /**
     * 查询车辆信息列表
     *
     * @return 车辆信息
     */
    @Override
    public List<VehiclesVo> selectPcVehiclesList(VehiclesSearchVo vehiclesSearchVo) {
        Long userId = SecurityUtils.getUserId();
        if(userId!=1){
            vehiclesSearchVo.setAgentUser(userId.toString());
        }
        if (vehiclesSearchVo.getAgentUser() != null) {
            AgentStoreCar agentStoreCar = new AgentStoreCar();
            agentStoreCar.setUserId(vehiclesSearchVo.getAgentUser());
            List<AgentStoreCar> agentStoreCars = agentStoreCarService.selectAgentStoreCarList(agentStoreCar);
            List<String> ids = new ArrayList<>();
            for (AgentStoreCar storeCar : agentStoreCars) {
                if (storeCar.getCarId() != null) {
                    ids.add(storeCar.getCarId());
                }
            }
            if (ids.size() == 0) {
                return new ArrayList<>();
            }
            vehiclesSearchVo.setCarIds(ids.toArray(new String[ids.size()]));
        }
        PageDomain pageDomain = new PageDomain();
        org.springframework.beans.BeanUtils.copyProperties(vehiclesSearchVo, pageDomain);
        startPage(pageDomain);
        List<VehiclesVo> vehiclesVos = vehiclesMapper.selectPcVehiclesList(vehiclesSearchVo);
        vehiclesVos.forEach(vehicles -> {
            if (vehicles.getApplyUser() != null) {
                if (sysUserService.selectUserById(Long.valueOf(vehicles.getApplyUser())) != null) {
                    vehicles.setApplyUserName(sysUserService.selectUserById(Long.valueOf(vehicles.getApplyUser())).getNickName());
                }
            }
        });
        return vehiclesVos;
    }

    @Override
    public List<VehiclesVo> selectList(VehiclesSearchVo vehiclesSearchVo) {
        if (vehiclesSearchVo.getAgentUser() != null) {
            AgentStoreCar agentStoreCar = new AgentStoreCar();
            agentStoreCar.setUserId(vehiclesSearchVo.getUserId());
            List<AgentStoreCar> agentStoreCars = agentStoreCarService.selectAgentStoreCarList(agentStoreCar);
            List<String> ids = new ArrayList<>();
            for (AgentStoreCar storeCar : agentStoreCars) {
                if (storeCar.getCarId() != null) {
                    ids.add(storeCar.getCarId());
                }
            }
            if (ids.size() == 0) {
                return new ArrayList<>();
            }
            vehiclesSearchVo.setCarIds(ids.toArray(new String[ids.size()]));
        }
        List<VehiclesVo> vehiclesVos = vehiclesMapper.selectPcVehiclesList(vehiclesSearchVo);
        vehiclesVos.forEach(vehicles -> {
            if (vehicles.getApplyUser() != null) {
                if (sysUserService.selectUserById(Long.valueOf(vehicles.getApplyUser())) != null) {
                    vehicles.setApplyUserName(sysUserService.selectUserById(Long.valueOf(vehicles.getApplyUser())).getNickName());
                }
            }
        });
        return vehiclesVos;
    }

    /**
     * 新增车辆信息
     *
     * @param vehicles 车辆信息
     * @return
     */
    @Override
    public R insertPcVehicles(Vehicles vehicles) {
        vehicles.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        return vehiclesMapper.insertPcVehicles(vehicles) > 0 ? R.ok("新增成功") : R.fail("操作失败");
    }


    /**
     * 修改车辆信息
     *
     * @param vehicles 车辆信息
     * @return 结果
     */
    @Override
    public R updatePcVehicles(Vehicles vehicles) {
        vehicles.setUpdateUser(String.valueOf(SecurityUtils.getUserId()));
        if (vehicles.getApplyType() != 0) {
            vehicles.setApplyUser(SecurityUtils.getUserId());
        }
        return vehiclesMapper.updatePcVehicles(vehicles) > 0 ? R.ok("修改成功") : R.fail("操作失败");
    }


    /**
     * 删除车辆信息信息
     *
     * @param id 车辆信息主键
     * @return 结果
     */
    @Override
    public R deletePcVehiclesById(String id) {
        return vehiclesMapper.deletePcVehiclesById(id) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }

    @Override
    public R deletePcVehiclesByIds(String[] id) {
        return vehiclesMapper.deletePcVehiclesByIds(id) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }

    /**
     * 添加产品或者服务
     *
     * @param productInsertVo
     * @return
     */
    @Override
    public R insertVehiclesProductOrServer(ProductInsertVo productInsertVo) {
        if (productInsertVo.getCategoryType() == 0) {
            Product product = productMapper.selectProductById(productInsertVo.getProductId());
            VehiclesProduct vehiclesProduct = new VehiclesProduct();
            vehiclesProduct.setProductId(product.getId());
            vehiclesProduct.setVehiclesId(productInsertVo.getStoreId());
            vehiclesProduct.setStorePrice(product.getStorePrice());
            vehiclesProduct.setGroupBuyPrice(product.getStorePrice());
            vehiclesProduct.setCreateUser(String.valueOf(SecurityUtils.getUserId()));
            if (appletVehiclesProductMapper.selectAppletVehiclesProductById(productInsertVo.getProductId(), productInsertVo.getStoreId()) != null) {
                return R.fail("该产品已添加");
            } else {
                appletVehiclesProductMapper.insertAppletVehiclesProduct(vehiclesProduct);
                return R.ok("添加成功");
            }
        } else if (productInsertVo.getCategoryType() == 1) {
            AppletServer appletServer = appletServerMapper.selectAppletServerById(productInsertVo.getProductId());
            AppletVehiclesServer appletVehiclesServer = new AppletVehiclesServer();
            appletVehiclesServer.setServerId(appletServer.getId());
            appletVehiclesServer.setVehiclesId(productInsertVo.getStoreId());
            appletVehiclesServer.setStorePrice(appletServer.getStorePrice());
            appletVehiclesServer.setGroupBuyPrice(appletServer.getStorePrice());
            appletVehiclesServer.setCreateUser(String.valueOf(SecurityUtils.getUserId()));
            if (appletVehiclesServerMapper.selectAppletVehiclesServerById(productInsertVo.getProductId(), productInsertVo.getStoreId()) != null) {
                return R.fail("该服务已添加");
            } else {
                appletVehiclesServerMapper.insertAppletVehiclesServer(appletVehiclesServer);
                return R.ok("添加成功");
            }
        }
        return R.fail("添加失败");
    }

    /**
     * 查询车辆下的产品或者服务
     */
    @Override
    public R selectVehiclesProductOrServer(StoreProductServerVo storeProductServerVo) {
        PageHelper.startPage(storeProductServerVo.getPageNum(), storeProductServerVo.getPageSize());
        List<StoreProductServer> storeProductServers = appletVehiclesProductMapper.selectVehiclesProductServerList(storeProductServerVo);
        PageInfo<StoreProductServer> pageInfo = new PageInfo<>(storeProductServers);
        return R.ok(pageInfo);
    }

    /**
     * 删除车辆下的产品或者服务
     * @param productDeleteVo
     * @return
     */
    @Override
    public R deleteVehiclesProductOrServer(ProductDeleteVo productDeleteVo) {
        List<ProductIdsVo> productIdsVoList = productDeleteVo.getProductIdsVoList();
        for (ProductIdsVo productIdsVo : productIdsVoList) {
            if (productIdsVo.getCategoryType() == 0){
                appletVehiclesProductMapper.deleteVehiclesProductByProductId(productIdsVo.getId(),productDeleteVo.getStoreId());
            }else {
                appletVehiclesServerMapper.deleteVehiclesServerByServerId(productIdsVo.getId(), productDeleteVo.getStoreId());
            }
        }
        return R.ok("删除成功");
    }


}
